<?php

namespace app\api\model;


class Theme extends BaseModel
{

    protected $hidden = ['update_time','delete_time','topic_img_id','head_img_id'];
    //定义关联关系
    public function topicImg()
    {/*一对一*/
//        $this->hasOne();
        /*如果一个表或模型中包含外键使用belongsTo,否则hasOne*/
        return $this->belongsTo('Image','topic_img_id','id');
    }

    public function headImg()
    {
        return $this->belongsTo('Image','head_img_id','id');
    }

    public function products()
    {
        return $this->belongsToMany('Product','theme_product','product_id','theme_id');
    }

    public static function getThemeWithProducts($id)
    {
        $theme = self::with(['products','topicImg','headImg'])
            ->find($id);
        return $theme;
    }
}
